#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2018/3/5 13:56
# @Author   : BYF
# file      : apaprment_list_case.py
# @version  :

import unittest, time, ddt
from base_test import BaseTest
from page.apartment_list_page import ApartmentListPage
from page.login_page import LoginPage
from common import function
from common.test_data_models import ApartmentlistTestDatum as ATD
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


@ddt.ddt
class ApartmentListTest(BaseTest):
    """选择门店测试"""

    # 获取测试数据
    DB = function.connect_db()
    test_data = DB.query(
        ATD.apartment_name,
        # ATD.excepted,
        ATD.screenshot,
    )
    test_data = tuple(test_data)

    # 选择门店验证
    def apartment_list_verify(self, apartment_name):
        ApartmentListPage(self.driver, self.config).choose_apartment(apartment_name)

    # 测试用例
    @ddt.data(*test_data)
    @ddt.unpack
    def test_apartment_list(self, apartment_name, screenshot):
        """选择门店测试"""

        self.apartment_list_verify(apartment_name)
        WebDriverWait(self.driver, 30, 0.5).until(EC.presence_of_element_located(ApartmentListPage.user_loc))

        try:
            # self.assertEqual(self.driver.current_url, expected)
            self.assertEqual(ApartmentListPage(self.driver, self.config).get_user_loca_name(), apartment_name)
            # 添加cookie到config
            cookies = self.driver.get_cookies()
            if len(cookies) > 0:
                self.function.set_cookies(cookies)
        except Exception as e:
            self.function.get_images(self.driver, screenshot)
            raise e


if __name__ == "__main__":
    unittest.main()
